#!/usr/bin/python
# -*- coding: UTF-8 -*-

"""
@company:ZYF
@author:yu.qian
@datetime:2022/10/28
"""

from commons.exceptions.base import ApiError


class UnknownError(ApiError):
    def __init__(self, msg=None, *args, **kwargs):
        self.status = -1
        self.code = -1
        self.msg = msg if msg else 'Server Error.'

        super(UnknownError, self).__init__(-1, -1, msg, *args, **kwargs)


class RequestError(ApiError):
    """
    请求错误基类，错误码必须落于1~99之间，包含1和99

    处理诸如：AccessToken缺失或不存在、请求签名错误、请求参数不足、...等请求错误。
    """

    _CodeMin = 0
    _CodeMax = 99

    def __init__(self, status=-1, code=_CodeMin, msg=None, *args, **kwargs):
        super(RequestError, self).__init__(status, code, msg, *args, **kwargs)
        if code < self._CodeMin or code > self._CodeMax:
            raise ValueError('Request error code must between %s to %s.' % (
                self._CodeMin, self._CodeMax))
        self.status = status
        self.code = code if code else self._CodeMin
        self.msg = msg if msg else 'Request Error.'


class AuthorizationError(ApiError):
    """
    授权错误基类，错误码必须落于100~199之间，包含100和199

    处理诸如：用户不存在或已存在、密码错误、登录/注册失败、用户/角色权限不足等授权错误。
    """

    _CodeMin = 100
    _CodeMax = 199

    def __init__(self, status=-1, code=_CodeMin, msg=None, *args, **kwargs):
        super(AuthorizationError, self).__init__(status, code, msg, *args, **kwargs)
        if code < self._CodeMin or code > self._CodeMax:
            raise ValueError('Authorization error code must between %s to %s.' % (
                self._CodeMin, self._CodeMax))
        self.status = status
        self.code = code if code else self._CodeMin
        self.msg = msg if msg else 'Authorization Error.'


class ResourceError(ApiError):
    """
    资源错误基类，错误码必须落于200~299之间，包含200和299

    处理诸如：资源不存在、请求资源过于频繁、上传资源格式不正确等资源异常。
    """
    _CodeMin = 200
    _CodeMax = 299

    def __init__(self, status=-1, code=_CodeMin, msg=None, *args, **kwargs):
        super(ResourceError, self).__init__(status, code, msg, *args, **kwargs)
        if code < self._CodeMin or code > self._CodeMax:
            raise ValueError('Resource error code must between %s to %s.' % (
                self._CodeMin, self._CodeMax))
        self.status = status
        self.code = code if code else self._CodeMin
        self.msg = msg if msg else 'Resource Error.'


class OperationError(ApiError):
    """
    用户操作错误基类，错误码必须落于300~399之间，包含300和399

    处理诸如：用户输入密码格式错误，两次密码不一致，缺少必要参数等错误。
    """

    _CodeMin = 300
    _CodeMax = 399

    def __init__(self, status=-1, code=_CodeMin, msg=None, *args, **kwargs):
        super(OperationError, self).__init__(status, code, msg, *args, **kwargs)
        if code < self._CodeMin or code > self._CodeMax:
            raise ValueError('Operation error code must between %s to %s.' % (
                self._CodeMin, self._CodeMax))
        self.status = status
        self.code = code if code else self._CodeMin
        self.msg = msg if msg else 'Operation Error.'


class ValidationError(ApiError):
    """
    数据检查错误基类，错误码必须落于400~499之间，包含400和499

    处理诸如：用户输入密码格式错误，两次密码不一致，缺少必要参数等错误。
    """

    _CodeMin = 400
    _CodeMax = 499

    def __init__(self, status=-1, code=_CodeMin, msg=None, *args, **kwargs):
        super(ValidationError, self).__init__(status, code, msg, *args, **kwargs)
        if code < self._CodeMin or code > self._CodeMax:
            raise ValueError('Validation error code must between %s to %s.' % (
                self._CodeMin, self._CodeMax))
        self.status = status
        self.code = code if code else self._CodeMin
        self.msg = msg if msg else 'Validate Error.'


class AppRequestError(RequestError):
    """
    请求错误基类，错误码必须落于1~99之间，包含1和99

    处理诸如：AccessToken缺失或不存在、请求签名错误、请求参数不足、...等请求错误。
    """

    TokenLost = RequestError._CodeMin + 2  # Token缺失
    SignError = RequestError._CodeMin + 3  # 签名错误
    ParamLost = RequestError._CodeMin + 4  # 参数缺失
    ParamFormat = RequestError._CodeMin + 5  # 参数格式错误
    InvalidToken = RequestError._CodeMin + 6  # 无效Token
    ParamRange = RequestError._CodeMin + 7  # 参数超出合法范围
    InvalidTS = RequestError._CodeMin + 8  # 无效的时间戳
    ImageFormat = RequestError._CodeMin + 9  # 无效的图片格式
    ParamDecryptError = RequestError._CodeMin + 10  # 参数解密失败
    ExportError = RequestError._CodeMin + 11  # 导出异常
    ParamTypeError = RequestError._CodeMin + 12  # 参数类型错误


class AppAuthorizationError(AuthorizationError):
    """
    授权错误基类，错误码必须落于100~199之间，包含100和199

    处理诸如：用户不存在或已存在、密码错误、登录/注册失败、用户/角色权限不足等授权错误。
    """

    InvalidToken = AuthorizationError._CodeMin + 1  # 无效的token
    ExpireToken = AuthorizationError._CodeMin + 2  # Token过期
    PwdError = AuthorizationError._CodeMin + 3  # 密码错误
    UserNotExists = AuthorizationError._CodeMin + 4  # 用户不存在
    UserNameNotExists = AuthorizationError._CodeMin + 5  # 用户名不存在
    InvalidSign = AuthorizationError._CodeMin + 6  # 秘钥签名非法
    UserNameFound = AuthorizationError._CodeMin + 7  # 用户名已存在
    WXAuthError = AuthorizationError._CodeMin + 8  # 微信授权失败
    PlatformNotFound = AuthorizationError._CodeMin + 9  # 平台不存在
    RoleCodeNotFound = AuthorizationError._CodeMin + 10  # 角色编码不存在
    UserCreateError = AuthorizationError._CodeMin + 11  # 用户创建失败


class AppResourceError(ResourceError):
    """
    资源错误基类，错误码必须落于200~299之间，包含200和299

    处理诸如：资源不存在、请求资源过于频繁、上传资源格式不正确等资源异常。
    """

    NotFound = ResourceError._CodeMin + 1  # 资源不存在
    TooBusy = ResourceError._CodeMin + 2  # 请求过于频繁
    InvalidFormat = ResourceError._CodeMin + 3  # 上传资源格式不正确
    CreateError = ResourceError._CodeMin + 4  # 创建失败
    UpdateError = ResourceError._CodeMin + 5  # 更新操作失败
    UnlinkError = ResourceError._CodeMin + 6  # 删除操作失败
    PermissionDeny = ResourceError._CodeMin + 7  # 没有权限
    UploadError = ResourceError._CodeMin + 8  # 上传失败


class AppValidationError(ValidationError):
    """
    数据检查错误基类，错误码必须落于400~499之间，包含400和499

    处理诸如：用户输入密码格式错误，两次密码不一致，缺少必要参数等错误。
    """

    OrgEmpty = ValidationError._CodeMin + 1  # 公司名为空
    UserNameEmpty = ValidationError._CodeMin + 2  # 用户名为空
    PwdEmpty = ValidationError._CodeMin + 3  # 密码为空
    UserEmailNotExists = ValidationError._CodeMin + 4  # 用户邮箱不存在
    CaptchaError = ValidationError._CodeMin + 5  # 验证码错误
    SimplePasswd = ValidationError._CodeMin + 6  # 密码过于简单
    UserIdEmpty = ValidationError._CodeMin + 7  # 用户id不能为空
    PhoneError = ValidationError._CodeMin + 8  # 手机号格式错误
    PwdneqError = ValidationError._CodeMin + 9  # 密码不一致
    PwdpatternError = ValidationError._CodeMin + 10  # 密码格式错误
    EmailError = ValidationError._CodeMin + 11  # 邮箱格式错误
    PhoneExistsError = ValidationError._CodeMin + 12  # 手机号已存在
    EmailExistsError = ValidationError._CodeMin + 13  # 邮箱已存在
    VcodeError = ValidationError._CodeMin + 14  # 短信验证码错误
    EmailServerDisabled = ValidationError._CodeMin + 15  # 邮件服务器不可用
    DataDuplicate = ValidationError._CodeMin + 16  # 编号重复不可用
    NameDuplicate = ValidationError._CodeMin + 17  # 名称重复
    OIDError = ValidationError._CodeMin + 18  # 不合法ID
    InvalidateUrl = ValidationError._CodeMin + 19  # 资源非法
    DataTypeError = ValidationError._CodeMin + 20  # 数据类型非法
